package com.eascs.admin.o2o.oper.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanUserService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.oper.entity.O2oOperationUploanlog;
import com.eascs.admin.o2o.oper.entity.O2oOperationUploanlogForm;
import com.eascs.admin.o2o.oper.service.IO2oOperationUploanlogService;
import com.eascs.admin.o2o.oper.vo.O2oOperUpVo;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.datalist.service.O2oDataListService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;

@Controller
@RequestMapping("/oper_up")
public class O2oOperUpController extends BaseMasterViewController{
	@Override
	public void preHandle() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "systemSettings");
	}
	@Reference
	IO2oOperationUploanlogService io2oOperationUploanlogService;
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	@Autowired
	private O2oDataListService o2oDataListService;
	@Autowired
	private O2oLoanUserService o2oLoanUserService;
	
	public String getPageResultOper(O2oOperUpVo operUpVo){
		
		
		return "";
	}
	
	@RequestMapping("/index")
	public String queryByPage(O2oOperationUploanlogForm form){
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "OPER_UPINDEX");
		form.setPageSize(10);
		String o2ourl = "/oper_up/index";
		String upUserName = form.getUpUserName() == null ? "" : form.getUpUserName();
		String upStartTime = form.getUpStartTime() == null ? "" : form.getUpStartTime();
		String upEndTime = form.getUpEndTime() == null ? "" : form.getUpEndTime();
		String billCode = form.getBillCode() == null ? "" : form.getBillCode();
		o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
		o2ourl = UrlUtil.appendUrl(o2ourl, "upUserName", upUserName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "upStartTime", upStartTime);
		o2ourl = UrlUtil.appendUrl(o2ourl, "upEndTime", upEndTime);
		o2ourl = o2ourl + "&pageIndex=";
		String titles[] = { "贷款单号", "操作人", "操作时间", "运维修改备注"};
		PageResult<Object[]> pageResult = io2oOperationUploanlogService.queryBypage(form);
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setBuildPagingNavigation(true);
		o2oPagingInfo.setFirstRenderColumnIndex(0);
		ViewContext.put("vo", form);
		ViewContext.put("o2oPagingInfo", o2oPagingInfo);
		return "oper/index";
	}
	
	/**
	 * 修改详情页面
	 * 
	 * @param loanapplyNo
	 * @param massege
	 * @return
	 */
	@RequestMapping(value = "/findloan")
	public String loanfile(String loanapplyNo, String massege, String type) {
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyNo);
		/**** 查询附件 ****/
		List<O2oDataListEntity> o2oDataList = o2oDataListService.findByEntityList(loanapplyNo);
		O2oLoanUserEntity  customer =  o2oLoanUserService.findById(o2oLoanapplyEntity.getLoanId(),loanapplyNo);
		O2oLoanUserEntity customerMate =  o2oLoanUserService.findSpouseId(o2oLoanapplyEntity.getLoanId(),loanapplyNo);
		if( customerMate!=null){
			if (customer != null && customerMate != null) {
				ViewContext.put("materBasic", customerMate);
			}
		}
		if (StringUtil.isNotBlank(type) && "detail".equals(type)) {
			O2oOperationUploanlog uploanlog = io2oOperationUploanlogService.queryByLoanapplyNo(loanapplyNo);
			if (null != uploanlog) {
				ViewContext.put("upRemart", uploanlog.getUpRemart());
			}
		}
		ViewContext.put("type", type);
		ViewContext.put("o2oDataList", o2oDataList);
		ViewContext.put("errorMassege", massege);
		ViewContext.put("loanApplyFileEntity", o2oLoanapplyEntity);
		return "oper/loanfile";
	}
}
